﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SewerAPI.Core
{
    /// <summary>
    /// Channel class
    /// </summary>
    [Serializable]
    public sealed class Channel
    {
        #region Private Fields

        string name = string.Empty;
        int sent = 0;
        int received = 0;
        StatisticsCollection statistics = new StatisticsCollection();

        #endregion

        #region Public Fields

        /// <summary>
        /// A unique channel name
        /// </summary>
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        /// <summary>
        /// Num of messages sent
        /// </summary>
        public int Sent 
        {
            get { return statistics.Where(x => x.Transaction == MessageTransaction.Sent).Count(); }
        }

        /// <summary>
        /// Received
        /// </summary>
        public int Received
        {
            get { return statistics.Where(x => x.Transaction == MessageTransaction.Received).Count(); }
        }

        /// <summary>
        /// Check if this channel object is empty
        /// </summary>
        public bool IsEmpty
        {
            get {
                return string.IsNullOrEmpty(Name) && Sent == 0 && Received == 0;
            }
        }

        #endregion

        #region Util Methods

        /// <summary>
        /// Register a message transaction
        /// </summary>
        /// <param name="m"></param>
        public void RegisterTransaction(Message m)
        {
            if (m.Transaction == MessageTransaction.Received || m.Transaction == MessageTransaction.Sent)
            {
                statistics.Add(Statistic.Create(m));
            }
        }

        /// <summary>
        /// Convert to string
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            string _s = string.Format("{0} {1}: ", Resources.Strings.CHANNEL, Name);
            if (IsEmpty) _s += Resources.Strings.EMPTY;
            else
            {
                _s += string.Format("{0}: {1}", Resources.Strings.SENT, Sent);
                _s += string.Format("{0}: {1}", Resources.Strings.RECEIVED, Received);
            }
            return _s;
        }

        #endregion
    }
}
